// pages/order/getOrder/getOrder.js
const handler = require("../../../utils/function.js");
var app = getApp(),
    api = require("../../../api.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
      img: handler.driverImg,
        time: 10,
        order: {},
        latitude: '',
        longitude: '',
        speed: '',
        accuracy: '',
        is_stop: 0,
        imgs: handler.userImg,
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var iscle = 0;
        this.timeTsk(iscle)
        this.getone();
    },
    getone: function() {
        const that = this;
        wx.getLocation({
            type: 'gcj02',
            success: function(res) {
                console.log('获取经纬度', res)
                app.request({
                    url: api.order.getOrder,
                    method: 'post',
                    data: {
                        latitude: res.latitude,
                        longitude: res.longitude,
                        speed: res.speed,
                        accuracy: res.accuracy,
                    },
                    success: function(rs) {
                        if (rs.code == 0) {
                            wx.showToast({
                                title: rs.msg,
                                icon: 'none',
                            })
                            wx.reLaunch({
                              url: '../../index/index'
                            })
                        } else {
                           wx.setStorageSync("is_stop",0)
                            that.setData({
                                order: rs.data,
                            })
                             clearTimeout(that.timeTsk);
                        }
                    }
                })
            }
        })
    },
    // 倒计时
    timeTsk: function() {
        const time = setInterval(() => {
            if (this.data.time > 0) {
                this.setData({
                    time: --this.data.time
                });
            } else {
                if ( wx.getStorageSync("is_stop") == 1) {
                    clearTimeout(time);
                } else {
                    this.getone();
                    this.setData({
                        time: 10
                    });
                }
            }
        }, 1000);
    },
    // intoPageHandler 页面跳转
    intoPageHandler: function(e) {
        var that = this;
        app.request({
            url: api.order.grabsOrder,
            method: 'post',
            data: {
                order_no: e.currentTarget.dataset.order_no,
            },
            success: function(rs) {
                wx.setStorageSync("is_stop",1)
                if (rs.code == 1) {
                    wx.setStorageSync("current_order", this.data.order)
                    wx.redirectTo({
                      url: e.currentTarget.dataset.skip,
                    })
                    // handler.intoPageHandler()
                } else {
                    wx.showToast({
                        title: rs.msg,
                        duration: 1000
                    })
                }
            }
        })
        // console.log(e.currentTarget.dataset.skip);
    },
    // 
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this;
        /*  app.request({
              url: api.order.getOrder,
              method: 'post',
              data: {
                  latitude: that.data.latitude,
                  longitude: that.data.longitude,
                  speed: that.data.speed,
                  accuracy: that.data.accuracy,
              },
              success: function(rs) {
                  console.log('未接单订单', rs)
              }
          })
          const order = wx.getStorageSync("order");
          var current_order = null;
          for (var i = 0; i < order.length; i++) {
              console.log(order[i].status);
              if (order[i].status === 6) {
                  console.log(order[i]);
                  current_order = order[i];
              }
          }
          this.setData({
              order: current_order
          });*/
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {}
})